import Vue from 'vue'
import Router from 'vue-router'
import Index from "../components/Index";
import Login from "../components/admin/Login"
import Home from "../components/admin/Home";
import Welcome from "../components/admin/Welcome"
import Users from "../components/admin/user/Users";

Vue.use(Router)


const router =new Router({
  routes: [
    {path:'/admin',redirect:'/admin/login'},
    {path:'/admin/login',component:Login},
    {path:'/index',component:Index},
    {
      path:'/admin/home',
      component:Home,
      redirect:'/admin/welcome',
      children:[
        {
          path:'/admin/welcome',
          component:Welcome,
        },
        {
          path:'/admin/user',
          component: Users,

        }
      ]
    }

  ],
  mode: 'history'
})

//挂在路由导航
router.beforeEach((to,from,next)=>{
  //to  将要访问的路径
  //from 从哪个路径来
  //next 是一个函数 表示放行
  if (to.path==='/admin/login') return next()

  const tokenStr=window.sessionStorage.getItem('token');

  if (!tokenStr) return  next('/admin/login');
  next();

})

export default router
